home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\FILE.C < prev    next >
C/C++ Source or Header  |  1994-12-31  |  3KB  |  168 lines

  1. /*
  2.  * file.c contains file open/read/write routines to be used to open
  3.  * files with the access permissions of the real UID instead of the
  4.  * effective UID. This allows IRCII to be run setuid->root. If it
  5.  * has effective UID == root, it will then use privileged ports to
  6.  * connect to servers, allowing the servers, some day, to take
  7.  * advantage of this information to ensure that the user names are
  8.  * who they claim to be.
  9.  *
  10.  * It can also be run setuid->something else, with ircserv being
  11.  * setuid->root and only runable by the given GID.
  12.  *
  13.  * Copyright (c) 1991 Troy Rollo
  14.  *
  15.  * See HELP IRCII COPYRIGHT for details.
  16.  */
  17.  
  18. #ifndef lint
  19. static    char    rcsid[] = "@(#)$Id: file.c,v 1.7 1994/07/02 02:32:13 mrg Stab $";
  20. #endif
  21.  
  22. #include "irc.h"
  23.  
  24. #include <sys/stat.h>
  25.  
  26. #ifdef PRIV_PORT
  27. # undef    PRIV_PORT
  28. #endif /* PRIV_PORT */
  29.  
  30. int    directory_writeable(file)
  31. char    *file;
  32. {
  33.     define_big_buffer(dir);
  34.     char    *ptr;
  35.     int    result;
  36.  
  37.     strmcpy(dir, file, BIG_BUFFER_SIZE);
  38.     if (ptr = rindex(dir, '/'))
  39.     {
  40.         if (ptr == dir
  41. #ifdef APOLLO
  42.             || (ptr == dir+1 && *dir == '/')
  43. #endif /*APOLLO*/
  44.             )
  45.             ptr++;
  46.         *ptr = '\0';
  47.     }
  48.     else
  49.         strcpy(dir, ".");
  50.     result = (!access(dir, W_OK|X_OK));
  51.     free_big_buffer(dir);
  52.     return result;
  53. }
  54.  
  55. int    ruid_open(filename, flags, mode)
  56. char    *filename;
  57. int    flags;
  58. int    mode;
  59. {
  60.     int    access_flags;
  61.     int    fd;
  62.  
  63.     switch(flags&(O_RDONLY|O_WRONLY|O_RDWR))
  64.     {
  65.     case O_RDWR:
  66.         access_flags = R_OK|W_OK;
  67.         break;
  68.     case O_RDONLY:
  69.         access_flags = R_OK;
  70.         break;
  71.     case O_WRONLY:
  72.         access_flags = W_OK;
  73.         break;
  74.     }
  75.     if (!access(filename, access_flags))
  76.         return open(filename, flags, mode);
  77.     else if ((flags&O_CREAT) == O_CREAT && directory_writeable(filename))
  78.     {
  79.         fd = open(filename, flags, mode);
  80.         chown(filename, getuid(), getgid());
  81.         return fd;
  82.     }
  83.     else
  84.         return -1;
  85. }
  86.  
  87.  
  88.  
  89. FILE    *ruid_fopen(filename, mode)
  90. char    *filename;
  91. char    *mode;
  92. {
  93.     int    access_flags;
  94.     FILE    *fp;
  95.     char    *tm;
  96.  
  97.     access_flags = 0;
  98.     for (tm = mode; *tm != '\0'; tm++)
  99.     {
  100.         switch (*tm)
  101.         {
  102.         case '+':
  103.             access_flags |= W_OK|R_OK;
  104.             break;
  105.         case 'r':
  106.             access_flags |= R_OK;
  107.             break;
  108.         case 'w':
  109.         case 'a':
  110.             access_flags |= W_OK;
  111.             break;
  112.         case 't':    /* Text and binary - harmless */
  113.         case 'b':
  114.             break;
  115.         default:     /* Calls are guilty unless proven innocent */
  116.             return NULL; /* :P to all those who think otherwise! */
  117.         }
  118.     }
  119.     if (!access(filename, access_flags))
  120.         return fopen(filename, mode);
  121.     else if ((access_flags&W_OK) == W_OK && directory_writeable(filename))
  122.     {
  123.         fp = fopen(filename, mode);
  124.         chown(filename, getuid(), getgid());
  125.         return fp;
  126.     }
  127.     else
  128.         return NULL;
  129. }
  130.  
  131. int    ruid_unlink(filename)
  132. char    *filename;
  133. {
  134.     if (!access(filename, W_OK) && directory_writeable(filename))
  135.         unlink(filename);
  136. }
  137.  
  138. int    ruid_system(command)
  139. char    *command;
  140. {
  141.     int    pid;
  142.  
  143.     switch (pid = fork())
  144.     {
  145.     case 0:
  146.         setuid(getuid());
  147.         setgid(getgid());
  148.         system(command);
  149.         _exit(0);
  150.         break;
  151.     case -1:
  152.         return -1;
  153.     default:
  154.         while(wait(0) != pid);
  155.         return 0;
  156.     }
  157. }
  158.  
  159. int    ruid_stat(path, buf)
  160. char    *path;
  161. struct    stat *buf;
  162. {
  163.     if (!access(path, 0))
  164.         return -1;
  165.     else
  166.         return stat(path, buf);
  167. }
  168.